package chapter2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by uu on 2017/6/14.
 */
public class Demo1_Hello_World_Producer {
    public static void main(String[] args){
        ConnectionFactory   factory =  new  ConnectionFactory();
        factory.setHost("localhost");
        try {
            //建立到服务器连接
            Connection connection = factory.newConnection();
            //获得信道
            Channel  channel = connection.createChannel();
            //声明交换器
            channel.exchangeDeclare("demo1_exchange", "direct", true);
            //声明队列
            channel.queueDeclare("demo1_queue", true, false, false, null);
            //通过key键将 队列和交换器绑定起来
            channel.queueBind("demo1_queue", "demo1_exchange", "demo1_queue");
            //发布消息
            byte[] messageBodyBytes = "Hello, world!".getBytes();
            channel.basicPublish("demo1_exchange", "demo1_queue", null, messageBodyBytes);
            System.out.println(" [x] Sent '" + "Hello, world!" + "'");
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

}
